#include "EagleLib.h"
#include "bt_at_cmd.h"
#include "Log.h"
#include "carplayExt.h"


class SdCarplayCallbacks : public ICarplayCallbacks
{
public:
    SdCarplayCallbacks(void *pctx) : mCtx(pctx) {}
    void carplaySessionStart();
    void carplaySessionStop();
    void iap2LinkStatus(int status);
    int switchUsbModeCB(UsbMode mode);
    void appleTimeUpdateCB(long long time);
    void appleLanguageUpdateCB(const char *lang);
    void appleCallStateUpdateCB(const char *remoteId,
    										const char *displayName,
    										int status,
    										int direction,
    										const char *uuid,
    										const char *addrBookId,
    										const char *label,
    										int service);
    void NotifyDeviceNameCB(const char *name, int name_len);
    void carplayExitCB();
    void returnNativeUICB();
    void modesChangeCB(CarPlayModeState *modes);
	void disableBluetoothCB();
	int iap2WriteData(char *buf, int len);

    void caplayDuckAudioCB(double inDurationSecs, double inVolume);
    void caplayUnduckAudioCB(double inDurationSecs);

    void carplayAudioStartCB(int handle, AudioStreamType type, int rate, int bits, int channels);
    void carplayAudioStopCB(int handle, AudioStreamType type);

    int carplayVideoStartCB();
    void carplayVideoStopCB();
    int carplayVideoDataProcCB(const char *buf, int len);

private:
    void *mCtx;
};

static SdCarplay            *carplayHandle;
static SdCarplayCallbacks   *carplayCBs;

void SdCarplayCallbacks::carplaySessionStart()
{
    LOGV("%s:%d\n", __func__, __LINE__);
}

void SdCarplayCallbacks::carplaySessionStop()
{
    LOGV("%s:%d\n", __func__, __LINE__);
    if (carplayHandle != nullptr) {
    	carplayHandle->stop();
    }
}
void SdCarplayCallbacks::iap2LinkStatus(int status)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}

int SdCarplayCallbacks::iap2WriteData(char *buf, int len)
{
    LOGV("%s:%d\n", __func__, __LINE__);
    return send_iap_data(buf, len);
}
int SdCarplayCallbacks::switchUsbModeCB(UsbMode mode)
{
    LOGV("%s:%d\n", __func__, __LINE__);

    return 0;
}
void SdCarplayCallbacks::appleTimeUpdateCB(long long time)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::appleLanguageUpdateCB(const char *lang)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::appleCallStateUpdateCB(const char *remoteId,
		const char *displayName,
		int status,
		int direction,
		const char *uuid,
		const char *addrBookId,
		const char *label,
		int service)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::NotifyDeviceNameCB(const char *name, int name_len)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::carplayExitCB()
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::returnNativeUICB()
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::modesChangeCB(CarPlayModeState *modes)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::disableBluetoothCB()
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::caplayDuckAudioCB(double inDurationSecs, double inVolume)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::caplayUnduckAudioCB(double inDurationSecs)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::carplayAudioStartCB(int handle, AudioStreamType type, int rate, int bits, int channels)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
void SdCarplayCallbacks::carplayAudioStopCB(int handle, AudioStreamType type)
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
int SdCarplayCallbacks::carplayVideoStartCB()
{
    LOGV("%s:%d\n", __func__, __LINE__);
    return 0;

}
void SdCarplayCallbacks::carplayVideoStopCB()
{
    LOGV("%s:%d\n", __func__, __LINE__);

}
int SdCarplayCallbacks::carplayVideoDataProcCB(const char *buf, int len)
{
    LOGV("%s:%d\n", __func__, __LINE__);
    return 0;

}

SdCarplay::LinkType g_carplay_type;

int init_carplay_sdk(){

    carplayCBs = new SdCarplayCallbacks(NULL);
    carplayHandle = new SdCarplay(carplayCBs);
    g_carplay_type = SdCarplay::CARPLAY_WL;

    carplayHandle->setValue("LINK_TYPE", SdCarplay::CARPLAY_WL);
//    carplayHandle->setValue("LINK_TYPE", g_carplay_type);//设置成无线carplay模式 CARPLAY_WL CARPLAY

    carplayHandle->setValue("NAME", "CarPlay System");
    carplayHandle->setValue("MODEID", "LinuxDongle");
    carplayHandle->setValue("MANFACTURER", "xiaoqiang");
    carplayHandle->setValue("SERIALNUMBER", "223000ac3987cf");
    carplayHandle->setValue("SW_VER", "sw1.1.0");
    carplayHandle->setValue("HW_VER", "hw1.1.0");
    carplayHandle->setValue("VEHICLE_NAME", "Audi");

    carplayHandle->setValue("OEM_ICON_VISIBLE", 1);
    carplayHandle->setValue("OEM_ICON_LABEL", "Home");
    carplayHandle->setValue("OEM_ICON_PATH", "/data/icon_120x120.png");

    carplayHandle->setValue("OSINFO", "Android13.0");
    carplayHandle->setValue("IOSVER_MIN", "11D257");
    carplayHandle->setValue("GUUID", "a527cd8d-7b0f-4135-674b-379f625a160d");
    carplayHandle->setValue("DEVID", "00:11:22:33:44:55");
    carplayHandle->setValue("BTMAC", "00:11:22:33:44:55");
    carplayHandle->setValue("RIGHTHAND_DRIVER", 0);
    carplayHandle->setValue("NIGHTMODE", 0);
    carplayHandle->setValue("HAVE_TELBUTTON", 0);
    carplayHandle->setValue("HAVE_MEDIABUTTON", 0);
    carplayHandle->setValue("HAVE_KNOB", 1);
    carplayHandle->setValue("HAVE_PROXSENSOR", 0);
    carplayHandle->setValue("HAVE_ENHANCED_REQCARUI", 0);
    carplayHandle->setValue("HAVE_ETCSUPPORTED", 0);
    carplayHandle->setValue("HIFI_TOUCH", 1);
    carplayHandle->setValue("LOFI_TOUCH", 0);
    carplayHandle->setValue("USB_COUNRY_CODE", 33);
    carplayHandle->setValue("PRODUCT_CODE", 0xa4a1);
    carplayHandle->setValue("VERNDOR_CODE", 0x0525);

    carplayHandle->setValue("I2C_NUM", 2);
    carplayHandle->setValue("I2C_ADDR", 0x22);

    carplayHandle->setValue("VIDEO_WIDTH", 1024);
    carplayHandle->setValue("VIDEO_HEIGHT", 600);
    carplayHandle->setValue("SCREEN_WIDTH_MM", 154);
    carplayHandle->setValue("SCREEN_HEIGHT_MM", 87);
    carplayHandle->setValue("FPS", 30);
	carplayHandle->setValue("SCREEN_WIDTH", 1024);
    carplayHandle->setValue("SCREEN_HEIGHT", 600);


	carplayHandle->setValue("IAP_PATH", "/dev/zjinnova_iap");

	carplayHandle->init();
    //char btmac[6] = {0xd0, 0x33, 0x11, 0x47, 0xc9, 0xac};
	//char btmac[6] = {0xb8, 0xc1, 0x11, 0x15, 0xe7, 0xea};
	char btmac[6] = {0x64, 0xb0, 0xa6, 0x30, 0x65, 0x3a};
	//char btmac[6] = {0xcc, 0x08, 0x8d, 0x44, 0x06, 0x41};

    carplayHandle->setBtAddr(btmac);//设置要连接手机的蓝牙地址
	carplayHandle->setValue("WIFI_SSID", "AndroidAP_28673");
    //carplayHandle->setValue("WIFI_SSID", "Weft");
	carplayHandle->setValue("WIFI_PASSWD", "12345678");

//	open_bt_uart();


    //carplayHandle->start(SdCarplay::CARPLAY, SdCarplay::Mode0);
//	carplayHandle->start(g_carplay_type, SdCarplay::Mode0);
    LOGV("%s:%d\n", __func__, __LINE__);
//    carplayHandle->stop();

//    delete(carplayHandle);
//    delete(carplayCBs);
	return 0;
}
int EagleLib_init(){
	LOGD( "%s:%d \n", __func__, __LINE__);
	open_bt_uart();
	init_carplay_sdk();
	return 0;
}

int EagleLib_exit(){
	LOGD( "%s:%d\n", __func__, __LINE__);
	return 0;
}




void connect_carplay_wl(char mac[6], char *local_mac, char bt_iap){
	if(carplayHandle != nullptr){
		carplayHandle->start(g_carplay_type, SdCarplay::Mode0);
	}
}
void connect_androidauto_wl(char mac[6], char *local_mac, char bt_iap){}
void on_bluetooth_connect_state(int state){}
void on_phone_bluetooth_name(char*name){}
int carplay_readIap2DataProc(char *buf, int len){
	if(carplayHandle != nullptr){
		return carplayHandle->readIap2DataProc(buf, len);
	}
	return 0;
}
int androidauto_readIap2DataProc(char *buf, int len){return 0;}
